/*
 * WRITTEN BY: Thomas Jakway
 * Project GLOnHill
 * http://code.google.com/p/gl-on-hill/
 * anonymous SVN checkout:
 * svn checkout http://gl-on-hill.googlecode.com/svn/trunk/ gl-on-hill-read-only
 * 
 * Demonstrates drawing an effects-filled scene in C++ with OpenGL
 * Textures are fetched for educational purposes only
 *
 *
 * Licensed under the GNU GPL v3, available from http://www.gnu.org/licenses/gpl-3.0.txt
 */

#ifndef RESIZE_AND_INIT
#define RESIZE_AND_INIT



//global header
#ifndef GLOBAL
#include "global.h"
#endif


#ifdef MAC_OS_X
#include <OpenGL/OpenGL.h>
#include <OpenGL/glu.h>
#endif

#ifdef LINUX
#include <GL/gl.h>
#include <GL/glu.h>
#endif

//standard C libraries
#include <stdio.h>
#include <stdlib.h>


#ifndef DRAW
#include "draw.h"
#endif


//our classes
#ifndef HILL_BLOCK_GROUP
#include "Hill_Block_Group.H"
#endif

#ifndef STAR
#include "Star.h"
#endif

//utility functions
#include "printProgramInfoLog.h"
#include "printShaderInfoLog.h"
#include "textFileWrite.h"
#include "ReadFile.h"


//a defined constant for the size of the texture array
//allows the addition of more textures by only changing this
//definition instead of having to change it across
//multiple files
#ifndef SIZE_OF_TEX_ARRAY
#define SIZE_OF_TEX_ARRAY 7
#endif

//C++ i/o streams
#include <iostream>
using namespace std;


/* function to reset our viewport after a window resize */
int resizeWindow( int w, int h )
{
	
    float ratio;
	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if(h == 0)
		h = 1;
	
	ratio = 1.0f * w / h;
	// Reset the coordinate system before modifying
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// Set the viewport to be the entire window
    glViewport(0, 0, w, h);
	
	// Set the clipping volume
	gluPerspective(80,ratio,1,200);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0, 0, 30,
		      0,0,10,
			  0.0f,1.0f,0.0f);
	
    return( true );
}


GLuint v, f, f2, p;

GLuint tex_id[SIZE_OF_TEX_ARRAY];




/* general OpenGL initialization function */
int initialize_OpenGL()
{
	//set the color to clear the color buffer bit to
	glClearColor(0, 0, 0, 0);
	
	

	//speed optimizations - enable depth testing and face culling
	glEnable(GL_DEPTH_TEST);
	//default depth function
	glDepthFunc(GL_LESS);
	//cull back faces
	glEnable ( GL_CULL_FACE );

	
	//normal array for the possible addition of lighting at a later time
	glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
	
	//enable 2D texturing and set pixel storage method
	glEnable ( GL_TEXTURE_2D );
	glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
	
	//hint for possibly nicer perspective calculations
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	
	/*  SHADER CODE - MAY BE USED LATER */
	 
	/*
	//a few strings
	// will hold onto the file read in!
	char *vs = NULL, *fs = NULL, *fs2 = NULL, *gs = NULL;
	
	//First, create our shaders 
	v = glCreateShader(GL_VERTEX_SHADER);
	f = glCreateShader(GL_FRAGMENT_SHADER);
	
	//Read in the programs
	vs = textFileRead("VertexShader.vert");
	fs = textFileRead("FragmentShader.frag");
	
	
	
	//Setup a few constant pointers for below
	const char * ff = fs;
	const char * vv = vs;
	
	glShaderSource(v, 1, &vv, NULL);
	glShaderSource(f, 1, &ff, NULL);
	
	free(vs);free(fs);
	
	glCompileShader(v);
	glCompileShader(f);
	
	
	p = glCreateProgram();
	
	glAttachShader(p,f);
	glAttachShader(p,v);
	
	
	glLinkProgram(p);
	glUseProgram(p);
	
	printShaderInfoLog(v, "VERTEX SHADER");
	printShaderInfoLog(f, "FRAGMENT SHADER");
	printProgramInfoLog(p);
	 
	 
	 */
	
	//load Star's texture
	Star::loadStarTexture();

	//generate texture ID's
	glGenTextures(SIZE_OF_TEX_ARRAY, tex_id);
	//load all textures
	tex_id[0] = loadTexturewithSDL("Data/colorful_flowers.jpg", GL_RGB);
	tex_id[1] = loadTexturewithSDL("Data/flower1.jpg", GL_RGB);
	tex_id[2] = loadTexturewithSDL("Data/flower2.jpg", GL_RGB);
	tex_id[3] = loadTexturewithSDL("Data/flower3.jpg", GL_RGB);
	tex_id[4] = loadTexturewithSDL("Data/flower4.jpg", GL_RGB);
	tex_id[5] = loadTexturewithSDL("Data/wood.jpg",   GL_RGB);
	tex_id[6] = loadTexturewithSDL("Data/door.jpg",   GL_RGBA);

	 //exit the function
    return( true );
}


#endif
